#coding=utf-8
'''
Created on 2017-6-15

@author: sandy
'''

#from twisted.internet.protocol import ReconnectingClientFactory
from twisted.internet.protocol import ClientFactory
from twisted.internet import reactor
from twisted.internet import protocol
import logging


class TestFactory(ClientFactory):
    def __init__(self,srv):
        #ReconnectingClientFactory.maxDelay = 5
        self.srv = srv
    
    def buildProtocol(self, addr):
        logging.warn(u"TestFactory buildProtocol!!!")
        #self.resetDelay()
        return self.srv

    def startedConnecting(self, connector):
        logging.warn(u"TestFactory startedConnecting!!!")
    
    def clientConnectionLost(self, connector, reason):
        logging.warn(u"TestFactory clientConnectionLost!!!")
        ClientFactory.clientConnectionLost(self, connector, reason)
        
    def clientConnectionFailed(self, connector, reason):
        logging.warn(u"TestFactory clientConnectionFailed!!!")
        ClientFactory.clientConnectionFailed(self, connector, reason)

class TestSrv(protocol.Protocol):
                      
    def __init__(self):
        self.client = None
        pass
    
    def start(self):
        reactor.connectTCP("127.0.0.1",7901,TestFactory(self))
        
    def stop(self):
        pass
    
    def connectionMade(self):
        logging.warn(u"TestSrv connectionMade()")

    def connectionLost(self, reason=protocol.connectionDone):
        logging.debug(u"TestSrv connectionLost()")
        
    def setClient(self,client):
        self.client = client

    def dataReceived(self, data):
        logging.debug(u"TestSrv dataReceived()")

    def sendPacket(self, data):
        self.transport.write(data)

instance = TestSrv()  
if __name__ == '__main__':
    pass